#pragma once

//对互斥锁的封装
#include<iostream>
#include<pthread.h>
#include<unistd.h>

class Mutex
{
public:
    Mutex()
    {
        //对锁进行初始化
        pthread_mutex_init(&_lock,nullptr);
    }
    //上锁
    void Lock()
    {
        pthread_mutex_lock(&_lock);
    }
    void Unlock()
    {
        pthread_mutex_unlock(&_lock);
    }
    pthread_mutex_t* Get()
    {
        return &_lock;
    }
    ~Mutex()
    {
        pthread_mutex_destroy(&_lock);
    }
private:
    pthread_mutex_t _lock;
};

class LockGuard
{
public:
    LockGuard(Mutex* _mutex):_mutexp(_mutex)
    {
        //对象创建时自动上锁
        _mutexp->Lock();
    }
    ~LockGuard()
    {
        //对象销毁的时候自动解锁
        _mutexp->Unlock();
    }
private:
    Mutex *_mutexp;
};